package com.yvan.dsf.tracing.client.support.rocketmq;

import com.alibaba.dubbo.common.utils.ConfigUtils;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.rocketmq.client.exception.MQClientException;
import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.yvan.YvanUtil;
import com.yvan.dsf.tracing.api.Span;
import com.yvan.dsf.tracing.api.TracingCollector;
import com.yvan.dsf.tracing.client.DstConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class RocketMqTracingCollector implements TracingCollector {

    private static final Logger logger = LoggerFactory.getLogger("yvan.dsf.trace");
    private DefaultMQProducer defaultMQProducer;

    public RocketMqTracingCollector() {
        defaultMQProducer = new DefaultMQProducer(DstConstants.ROCKET_MQ_PRODUCER);
        String nameSrvAddr = ConfigUtils.getProperty(DstConstants.ROCKET_MQ_NAME_SRV_ADD);
        if (!StringUtils.isEmpty(nameSrvAddr)) {
            defaultMQProducer.setNamesrvAddr(nameSrvAddr);
            try {
                defaultMQProducer.start();
            } catch (MQClientException e) {
                throw new IllegalArgumentException("fail to start rocketmq producer.", e);
            }
        }
    }

    @Override
    public void push(List<Span> spanList) {
        if (logger.isInfoEnabled()) {
            logger.info(YvanUtil.toJson(spanList));
        }
        /*
        byte[] bytes = YvanUtil.toJsonByte(spanList);
        Message message = new Message(DstConstants.ROCKET_MQ_TOPIC, bytes);
        try {
            SendResult sendResult = defaultMQProducer.send(message);
            if (sendResult.getSendStatus() != SendStatus.SEND_OK) {
                logger.error("send mq message return [" + sendResult.getSendStatus() + "]");
            }
        } catch (Exception e) {
            logger.error("fail to send message.", e);
        }
        */
    }
}
